﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using WMM.Tapioca.Linkbase;
using System.Text.RegularExpressions;

namespace WMM.Tapioca.Formula
{
    /// <summary>
    /// Relate to facts in a source instance document.
    /// </summary>
    public class FactVariable : Variable
    {       
        private bool nils;

        /// <returns>the @nils attribute to fact variables to ensure that fact variables 
        /// can determine their own ability to evaluate to nil facts. </returns>
        public bool Nils
        {
            get { return nils; }
            set { nils = value; }
        }

        private bool matches;

        /// <returns>Whether variables that can bind as a sequence can allow duplicate facts in a single
        /// evaluation sequence to controlling whether variables that can bind as a sequence can allow 
        /// aspect-matched facts in a single evaluation sequence. </returns>
        public bool Matches
        {
            get { return matches; }
            set { matches = value; }
        }

        private List<Filter> filters;

        /// <returns> List of Filters. </returns>
        public List<Filter> Filters
        {
            get { return filters; }
            set { filters = value; }
        }

        private List<string> coveredAspects;

        /// <returns> List of covered aspects. </returns>
        public List<string> CoveredAspects
        {
            get { return coveredAspects; }
            set { coveredAspects = value; }
        }

        List<Arc> variableFilterArcs;

        /// <returns> List of arcs with arcrole="http://xbrl.org/arcrole/2008/variable-filter". </returns>
        public List<Arc> VariableFilterArcs
        {
            get { return variableFilterArcs; }
            set { variableFilterArcs = value; }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public FactVariable(Resource resource)
            : base(resource)
        {
            VariableFilterArcs = new List<Arc>();
            filters = new List<Filter>();
            coveredAspects = new List<string>();

            foreach (XAttribute attr in resource.ResourceXElement.Attributes())
            {
                if (attr.Name.LocalName.Equals("nils"))
                {
                    nils = Boolean.Parse(attr.Value);
                }

                if (attr.Name.LocalName.Equals("matches"))
                {
                    matches = Boolean.Parse(attr.Value);
                }

                if (attr.Name.LocalName.Equals("fallbackValue"))
                {
                    FallbackString = attr.Value;
                    FallbackString = FallbackString.TrimEnd().TrimStart();
                    FallbackString = Regex.Replace(FallbackString, @"\s+", " ");

                    int result;

                    if (int.TryParse(EvaluateExpression(FallbackString), out result))
                    {
                        FallbackValue = result;
                    }
                    else
                    {
                        FallbackValue = 0;
                    }
                }
            }
        }

       
    }
}
